package com.mall.pay.services;

import com.alipay.demo.trade.Main;
import com.mall.pay.PayService;
import com.mall.pay.constants.PayRetCode;
import com.mall.pay.convertor.PayConvertor;
import com.mall.pay.dal.entitys.Payment;
import com.mall.pay.dal.persistence.PaymentMapper;
import com.mall.pay.dto.PayDTO;
import com.mall.pay.dto.SelectByIdRequest;
import com.mall.pay.dto.SelectByIdResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Service(interfaceClass = PayService.class)
@Component
public class PayServiceImpl implements PayService {


    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private PayConvertor payConvertor;

    @Autowired
    Main main;

    @Override
    public SelectByIdResponse selectById(SelectByIdRequest request) {

        SelectByIdResponse selectByIdResponse = new SelectByIdResponse();

        try{
            request.requestCheck();


            Payment payment = paymentMapper.selectByPrimaryKey(request.getId());

            PayDTO payDTO = payConvertor.paymentConvert2DTO(payment);

            selectByIdResponse.setPayDTO(payDTO);

            selectByIdResponse.setCode(PayRetCode.SUCCESS.getCode());
            selectByIdResponse.setMsg(PayRetCode.SUCCESS.getMessage());


        }catch (RuntimeException e){
            selectByIdResponse.setCode(PayRetCode.PARAM_IS_NULL.getCode());
            selectByIdResponse.setMsg(PayRetCode.PARAM_IS_NULL.getMessage());
            return selectByIdResponse;
        }
        catch (Exception e) {
            selectByIdResponse.setCode(PayRetCode.SYSTEM_ERROR.getCode());
            selectByIdResponse.setMsg(PayRetCode.SYSTEM_ERROR.getMessage());
            return selectByIdResponse;
        }

        main.test_trade_precreate("xxx");
        main.test_trade_query();

        return selectByIdResponse;
    }
}
